<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>分割回文串</title>
</head>

<body>

  <script>
    var partition = function (s) {
      //判断是否为回文字符串
      function isPalindrome(s, l, r) {
        for (let i = l, j = r; i < j; i++, j--) {
          if (s[i] !== s[j]) return false;
        }
        return true;
      }

      //回溯主体
      function backtraking(start) {
        //start的位置超出了字符串，这时候将切割的子串放入res中
        if (start >= s.length) {
          res.push([...path]);
          return;
        }
        for (let i = start; i < s.length; i++) {
          //s为所要分割的字符串，其子串区间为[start,i]，左闭右闭
          if (isPalindrome(s, start, i) == false) continue;
          //.slice()是左闭右开区间，所以i+1
          path.push(s.slice(start, i + 1));
          backtraking(i + 1);
          path.pop();
        }
      }

      //初始化条件
      const res = [], path = [];
      //调用回溯函数
      backtraking(0);
      return res;

    }
    console.log(partition("aab"))

  </script>
</body>

</html>